package com.example.testlibraryproject.utils;

import android.content.Context;
import android.content.SharedPreferences;

public class LibSharedPrefsUtils {

	/**
	 * Function to write a string value to shared prefs
	 * @param context
	 * @param key
	 * @param value
	 */
	public static void putStringSharedPrefs(Context context, String appSharedPrefsName, String key, String value){
		SharedPreferences settings = context.getSharedPreferences(appSharedPrefsName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		// Commit the edits!
		editor.commit();
	}
	/** Insert boolean value to shared prefs */
	public static void putBoolSharedPrefs(Context context, String appSharedPrefsName, String key, Boolean value){
		SharedPreferences settings = context.getSharedPreferences(appSharedPrefsName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}
	/** put long value to shared prefs */
	public static void putLongSharedPrefs(Context context, String appSharedPrefsName, String key, Long value){
		SharedPreferences settings = context.getSharedPreferences(appSharedPrefsName, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(key, value);
		editor.commit();
	}

	/** Return String value for key in application shared prefs*/
	public static String getStringSharedPrefs(Context context, String appSharedPrefsName, String key, String defaultValue){

		try {
			SharedPreferences settings = context.getSharedPreferences(appSharedPrefsName, Context.MODE_PRIVATE);
			return settings.getString(key, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultValue;
		}

	}

	/** Return boolean value for key in application shared prefs*/
	public static Boolean getBooleanSharedPrefs(Context context, String appSharedPrefsName, String key, boolean defaultValue){

		try {
			SharedPreferences settings = context.getSharedPreferences(appSharedPrefsName, Context.MODE_PRIVATE);
			return settings.getBoolean(key, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultValue;
		}

	}

	/** Return long value for key in application shared prefs*/
	public static Long getLongSharedPrefs(Context context, String appSharedPrefsName, String key, long defaultValue){

		try {
			SharedPreferences settings = context.getSharedPreferences(appSharedPrefsName, Context.MODE_PRIVATE);
			return settings.getLong(key, defaultValue);
		} catch (Exception e) {
			e.printStackTrace();
			return defaultValue;
		}

	}
}
